package lingnan.cn.edu.dialog;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import lingnan.cn.edu.BaseAndImpl.MilkManageService;
import lingnan.cn.edu.User.Milkteainf;
import lingnan.cn.edu.service.MilkManageServiceImpl;
import lingnan.cn.edu.view.ManageView;

public class MilkManageDialog extends JDialog implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = -399954889003353898L;

	private JPanel namePanel,pricePanel,opePanel,imgPanel,typePanel;
	
	private JLabel nameLabel,priceLabel,imgLabel;
	private JTextField nameTF,priceTF,imgTF;
	
	private JButton saveBtn,cancelBtn;
	
	private JRadioButton type1 = new JRadioButton("奶茶");
	private JRadioButton type2 = new JRadioButton("果茶");
	private JRadioButton type3 = new JRadioButton("牛奶冰沙");
	private JRadioButton type4 = new JRadioButton("奶盖");
	private JRadioButton type5= new JRadioButton("加料");
	
	private MilkManageService milkmanageService = new MilkManageServiceImpl();
	
	private ManageView manageView;
	
	
	private String title = "null";
	private String mteaName = "null";
	
	public MilkManageDialog(JFrame parent,ManageView manageView,String title) {
		super(parent,title);
		this.title = title;
		this.manageView = manageView;
		
		setSize(350,300);
		
		setLocationRelativeTo(null);
		
		setModal(true);
		setResizable(false);
		 
		this.setLayout(new FlowLayout());
		
		initView();
	}
	
	public MilkManageDialog(JFrame parent,ManageView manageView,String title,Milkteainf milkteainf) {
		super(parent,title);
		this.title = title;
		this.manageView = manageView;
		
		setSize(350,300);
		
		setLocationRelativeTo(null);
		
		setModal(true);
		setResizable(false);
		 
		this.setLayout(new FlowLayout());
		
		updateView(milkteainf);
	}
	
	private void initView() {
		ButtonGroup grp = new ButtonGroup();
		grp.add(type1);
		grp.add(type2);
		grp.add(type3);
		grp.add(type4);
		grp.add(type5);
		
		namePanel = new JPanel();
		nameLabel = new JLabel("名称");
		nameTF = new JTextField(15);
		namePanel.add(nameLabel);
		namePanel.add(nameTF);
				
		pricePanel = new JPanel();
		priceLabel = new JLabel("价格");
		priceTF = new JTextField(15);
		pricePanel.add(priceLabel);
		pricePanel.add(priceTF);
		
		imgPanel = new JPanel();
		imgLabel = new JLabel("图片");
		imgTF = new JTextField(15);
		imgPanel.add(imgLabel);
		imgPanel.add(imgTF);
		
		typePanel = new JPanel();
		typePanel.add(type1);
		typePanel.add(type2);
		typePanel.add(type3);
		typePanel.add(type4);
		typePanel.add(type5);
		
		opePanel = new JPanel();
		saveBtn = new JButton("保存");
		cancelBtn = new JButton("取消");
		saveBtn.addActionListener(this);
		cancelBtn.addActionListener(this);
		opePanel.add(saveBtn);
		opePanel.add(cancelBtn);
		
		Container container = getContentPane();
		container.add(namePanel);		
		container.add(pricePanel);
		container.add(imgPanel);
		container.add(typePanel);
		container.add(opePanel);
	}
	
	private void updateView(Milkteainf milkteainf) {
		ButtonGroup grp = new ButtonGroup();
		grp.add(type1);
		grp.add(type2);
		grp.add(type3);
		grp.add(type4);
		grp.add(type5);
		if(milkteainf.getType().equals("奶茶"))
			type1.setSelected(true);
		else if(milkteainf.getType().equals("果茶"))
			type2.setSelected(true);
		else if(milkteainf.getType().equals("牛奶冰沙"))
			type3.setSelected(true);
		else if(milkteainf.getType().equals("奶盖"))
			type4.setSelected(true);
		else
			type5.setSelected(true);
		
		namePanel = new JPanel();
		nameLabel = new JLabel("名称");
		nameTF = new JTextField(15);
		nameTF.setText(milkteainf.getName());
		mteaName = milkteainf.getName();
		namePanel.add(nameLabel);
		namePanel.add(nameTF);
		
		
		pricePanel = new JPanel();
		priceLabel = new JLabel("价格");
		priceTF = new JTextField(15);
		priceTF.setText(String.valueOf(milkteainf.getPrice()));
		pricePanel.add(priceLabel);
		pricePanel.add(priceTF);
		
		imgPanel = new JPanel();
		imgLabel = new JLabel("图片");
		imgTF = new JTextField(15);
		imgTF.setText(milkteainf.getImg());
		imgPanel.add(imgLabel);
		imgPanel.add(imgTF);
		
		typePanel = new JPanel();
		typePanel.add(type1);
		typePanel.add(type2);
		typePanel.add(type3);
		typePanel.add(type4);
		typePanel.add(type5);
		
		opePanel = new JPanel();
		saveBtn = new JButton("更新");
		cancelBtn = new JButton("取消");
		saveBtn.addActionListener(this);
		cancelBtn.addActionListener(this);
		opePanel.add(saveBtn);
		opePanel.add(cancelBtn);
		
		Container container = getContentPane();
		container.add(namePanel);
		container.add(pricePanel);
		container.add(imgPanel);
		container.add(typePanel);
		container.add(opePanel);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if (source == saveBtn) {
			String name = nameTF.getText();
			String price = priceTF.getText();
			String img = imgTF.getText();
			String type;
			if(type1.isSelected()) {
				type="奶茶";
			}else if(type2.isSelected()) {
				type = "果茶";
			}else if(type3.isSelected()) {
				type = "牛奶冰沙";
			}else if (type4.isSelected()) {
				type = "奶盖";
			}else {
				type = "加料";
			}
			Milkteainf milktea = new Milkteainf();
			milktea.setName(name);
			milktea.setPrice(Integer.parseInt(price));
			milktea.setImg(img);
			milktea.setType(type);
			if(title.equals("添加")) {				
				int result = milkmanageService.addMilkTea(milktea);
				if(result==1) {
					JOptionPane.showMessageDialog(this,"添加成功","提示",JOptionPane.INFORMATION_MESSAGE);
					this.dispose();
					//刷新
					manageView.refresh();
				}else {
					JOptionPane.showMessageDialog(this,"添加失败","提示",JOptionPane.ERROR_MESSAGE);
				}
			}else {
				int updateresult = milkmanageService.updateMilkTea(milktea, mteaName);
				if(updateresult==1) {
					JOptionPane.showMessageDialog(this,"更新成功","提示",JOptionPane.INFORMATION_MESSAGE);
					this.dispose();
					//刷新
					manageView.refresh();
				}else {
					JOptionPane.showMessageDialog(this,"更新失败","提示",JOptionPane.ERROR_MESSAGE);
				}
			}
			
		}else if(source == cancelBtn) {
			
			this.dispose();
		}
	}
}
